// 7zr.exe x lzma1900.7z  -oD://Rust/
extern crate execute;

use async_process;
use std::process::Command;

use execute::Execute;

// 解压
pub fn run_cmd_unpack(output_path: String, file_path: String, unchange_file_name: String) {
    const SEVENZ_PATH: &str = "./lib/7z.exe";

    let mut command = Command::new(SEVENZ_PATH);
    let mut unchange_file_name = unchange_file_name.clone();
    let  unchange_file_name_vec:Vec<&str> = unchange_file_name.split(".").collect();
    unchange_file_name = unchange_file_name_vec[unchange_file_name_vec.len()-2].to_string();

    command.arg("x");
    command.arg(&file_path);
    command.arg("-o".to_string() + &output_path + &unchange_file_name);

    let output = command.execute_output().unwrap();

    if let Some(exit_code) = output.status.code() {
        if exit_code == 0 {
            println!("{:?}", output);
            println!("Ok.");
        } else {
            eprintln!("Failed.");
        }
    } else {
        eprintln!("Interrupted!");
    }
}

// 安装 msi
pub fn run_cmd_install(file_path: String) {
    let mut file_path = file_path.clone();
    const MSIEXEC_PATH: &str = "C:/Windows/System32/msiexec.exe";
    let mut command = async_process::Command::new(MSIEXEC_PATH);
    file_path = file_path.replace("/", "\\");
    command.arg("/i");
    command.arg(&file_path);
    let output = command.output();
}

pub fn run_cmd_exe(file_path: String) {
    println!("file_path = {}",file_path);
    let mut file_path = file_path.clone();
    file_path = file_path.replace("/", "\\");
    let mut command = async_process::Command::new(&file_path);
    let output = command.output();
}
